﻿using System;
using System.Threading.Tasks;
using DataCenter.Models;
using System.Reflection;

namespace Client.Panels
{
    public class TickLinePanel : ILinePanel<DataCenter.Models.Tick>
    {
        public TickLinePanel(IInfo ggInfo) : base(ggInfo, "分时图 - " + ggInfo.name)
        {
            RegisterHotkey(panelName);
            beepTime = Managers.Manager.beepM.tickWav;
            beepEmptyTime = Managers.Manager.beepM.tickWavEmpty;

        }

        protected override async Task<int> InitLines()
        {
            try
            {
                data = await DataCenter.DataCenter.GetTickLine(ggInfo.symbol);
                if (ggInfo as StockInfo == null)
                {
                    var l = await DataCenter.DataCenter.GetSSSJ(new string[] { ggInfo.symbol });
                    ggInfo = l[0];
                }
            }
            catch (DataCenter.MyException.APIReturnException e)
            {
                var l = e.Message.Split('~');
                if (l[0] == "1000")
                    Utility.ShowLoginAgain();
                throw;
            }

            var fsLine = new YYBeep.Beep[data.Length];
            var aveLine = new YYBeep.Beep[data.Length];
            var volLine = new YYBeep.Beep[data.Length];
            var volArr = new float[data.Length];

            //获取最大成交量。分时图的成交量是累计的，data中的vol也需要重新计算
            float maxVol = data[0].vol;
            volArr[0] = data[0].vol;
            for(int i=1;i<data.Length;i++)
            {
                var vol = data[i].vol - data[i - 1].vol;
                volArr[i] = vol;
                if (vol > maxVol)
                    maxVol = vol;
            }
            for (int i = 0; i < volArr.Length; i++) data[i].vol = volArr[i];

            float total = 0;
            for (int i = 0; i < data.Length; i++)
            {
                total += data[i].trade;
                float pan = -100 + i * (200.0f / 240);
                var pitch = (data[i].trade - ((StockInfo)ggInfo).preclose) / ((StockInfo)ggInfo).preclose * 1000;
                var ave = total / (i + 1);
                var avePitch = (ave - ((StockInfo)ggInfo).preclose) / ((StockInfo)ggInfo).preclose * 1000;
                var volVolume = data[i].vol / maxVol;

                data[i].average = ave;
                fsLine[i] = YYBeep.Beep.CreateBeep(pitch, pan, 1, Math.Sin, Managers.Manager.beepM.generalWav);
                aveLine[i] = YYBeep.Beep.CreateBeep(avePitch, pan, 1, Math.Sin, Managers.Manager.beepM.generalWav);
                volLine[i] = YYBeep.Beep.CreateBeep(256, pan, volVolume, YYBeep.MyMath.SquareWav, Managers.Manager.beepM.generalWav);

            }
            lines.Add(fsLine);
            lines.Add(aveLine);
            lines.Add(volLine);

            return 0;
        }

        protected override string timeToText(DateTime time)
        {
            return time.ToString("h点m分，M月d日");
        }

        public override void Enter()
        {
            if (y < items[y].Count - 1)
                PlayLine(x, lines[y].Length - 1);
            else
                SpeakCell();
        }
        public override void Left()
        {
            beepTime = Managers.Manager.beepM.tickWav;
            beepEmptyTime = Managers.Manager.beepM.tickWavEmpty;
            base.Left();
        }
        public override void PageUp()
        {
            x -= int.Parse(Managers.Manager.settingM.fsSetting["numPerPage"]);
            Navigation(false);
            SpeakTime();
        }
        public override void PageDown()
        {
            x += int.Parse(Managers.Manager.settingM.fsSetting["numPerPage"]);
            Navigation(false);
            SpeakTime();
        }
        public override void SetEndPoint()
        {
            if (x > -1)
            {
                endPoint = x;
                PlayLine(startPoint, endPoint);
            }
        }

        //========================


        public void SpeakHighest()
        {
            var p = typeof(DataCenter.Models.Tick).GetProperty("trade", BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);
            int index = GetHighestAndLowest<float>(0, data.Length - 1, p);
            Managers.Manager.beepM.PlayBeep(lines[0][index]);
            Managers.Manager.ttsM.Speak("视图最高价" + data[index].trade.ToString("F2"));
        }
        public void JumpToHighest()
        {
            var p = typeof(DataCenter.Models.Tick).GetProperty("trade", BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);
            int index = GetHighestAndLowest<float>(0, data.Length - 1, p);
            x = index;
            Managers.Manager.beepM.PlayBeep(lines[0][index]);
            Managers.Manager.ttsM.Speak("跳转视图最高价");
        }
        public void SpeakLowest()
        {
            var p = typeof(DataCenter.Models.Tick).GetProperty("trade", BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);
            int index = GetHighestAndLowest<float>(0, data.Length - 1, p, false);
            Managers.Manager.beepM.PlayBeep(lines[0][index]);
            Managers.Manager.ttsM.Speak("视图最低价" + data[index].trade.ToString("F2"));
        }
        public void JumpToLowest()
        {
            var p = typeof(DataCenter.Models.Tick).GetProperty("trade", BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);
            int index = GetHighestAndLowest<float>(0, data.Length - 1, p, false);
            x = index;
            Managers.Manager.beepM.PlayBeep(lines[0][index]);
            Managers.Manager.ttsM.Speak("跳转视图最低价");
        }
        public void SpeakVolMax()
        {
            var p = typeof(DataCenter.Models.Tick).GetProperty("vol", BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);
            int index = GetHighestAndLowest<float>(0, data.Length - 1, p);
            Managers.Manager.beepM.PlayBeep(lines[2][index]);
            string str = "";
            if (data[index].vol > 100000000)
                str = (data[index].vol / 100000000).ToString("F2") + "亿";
            else if (data[index].vol > 10000)
                str = (data[index].vol / 10000).ToString("F2") + "万";
            Managers.Manager.ttsM.Speak("最大成交量" + str);
        }
        public void JumpToVolMax()
        {
            var p = typeof(DataCenter.Models.Tick).GetProperty("vol", BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);
            int index = GetHighestAndLowest<float>(0, data.Length - 1, p);
            x = index;
            Managers.Manager.beepM.PlayBeep(lines[2][index]);
            Managers.Manager.ttsM.Speak("跳转最大成交量");
        }

        public void SpeakRangeHighest()
        {
            var p = typeof(DataCenter.Models.Tick).GetProperty("trade", BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);
            int index = GetHighestAndLowest<float>(startPoint, endPoint, p);
            Managers.Manager.beepM.PlayBeep(lines[0][index]);
            Managers.Manager.ttsM.Speak("区间最高价" + data[index].trade.ToString("F2"));
        }
        public void JumpRangeToHighest()
        {
            var p = typeof(DataCenter.Models.Tick).GetProperty("trade", BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);
            int index = GetHighestAndLowest<float>(startPoint, endPoint, p);
            x = index;
            Managers.Manager.beepM.PlayBeep(lines[0][index]);
            Managers.Manager.ttsM.Speak("跳转区间最高点");
        }
        public void SpeakRangeLowest()
        {
            var p = typeof(DataCenter.Models.Tick).GetProperty("trade", BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);
            int index = GetHighestAndLowest<float>(startPoint, endPoint, p, false);
            Managers.Manager.beepM.PlayBeep(lines[0][index]);
            Managers.Manager.ttsM.Speak("区间最低价" + data[index].trade.ToString("F2"));
        }
        public void JumpRangeToLowest()
        {
            var p = typeof(DataCenter.Models.Tick).GetProperty("trade", BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);
            int index = GetHighestAndLowest<float>(startPoint, endPoint, p, false);
            x = index;
            Managers.Manager.beepM.PlayBeep(lines[0][index]);
            Managers.Manager.ttsM.Speak("跳转区间最低点");
        }
        public void SpeakRangeVolMax()
        {
            var p = typeof(DataCenter.Models.Tick).GetProperty("vol", BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);
            int index = GetHighestAndLowest<float>(startPoint, endPoint, p);
            Managers.Manager.beepM.PlayBeep(lines[2][index]);
            string str = "";
            if (data[index].vol > 100000000)
                str = (data[index].vol / 100000000).ToString("F2") + "亿";
            else if (data[index].vol > 10000)
                str = (data[index].vol / 10000).ToString("F2") + "万";
            Managers.Manager.ttsM.Speak("区间最大成交量" + str);
        }
        public void JumpRangeToVolMax()
        {
            var p = typeof(DataCenter.Models.Tick).GetProperty("vol", BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);
            int index = GetHighestAndLowest<float>(startPoint, endPoint, p);
            x = index;
            Managers.Manager.beepM.PlayBeep(lines[2][index]);
            Managers.Manager.ttsM.Speak("跳转区间最大成交量");
        }
    }
}
